{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from torchtext import data,datasets\n",
    "from torchtext.vocab import GloVe,FastText,CharNGram\n",
    "import torch.nn as nn\n",
    "import torch.optim as optim\n",
    "import torch.nn.functional as F\n",
    "from torch.autograd import Variable\n",
    "import torch\n",
    "from imdb import IMDB\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.6.2 |Anaconda custom (64-bit)| (default, Jul 20 2017, 13:51:32) \\n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.__version__\n",
    "sys.version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'utf-8'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sys.getdefaultencoding()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "is_cuda = False\n",
    "\n",
    "if torch.cuda.is_available():\n",
    "    is_cuda=True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "TEXT = data.Field(lower=True, batch_first=True,fix_length=40,)\n",
    "LABEL = data.Field(sequential=False,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train, test = IMDB.splits(TEXT, LABEL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "imdb.IMDB"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train.fields {'text': <torchtext.data.field.Field object at 0x7f6db748ef60>, 'label': <torchtext.data.field.Field object at 0x7f6db7c44630>}\n",
      "len(train) 25000\n",
      "vars(train[0]) {'text': [\"i'm\", 'not', 'tired', 'to', 'say', 'this', 'is', 'one', 'of', 'the', 'best', 'political', 'thrillers', 'ever', 'made.', 'the', 'story', 'takes', 'place', 'in', 'a', 'fictional', 'state,', 'but', 'obviously', 'it', 'deals', 'with', 'the', 'murder', 'of', 'kennedy.', 'a', 'truthful', 'and', 'honest', 'district', 'attorney', '(played', 'by', 'yves', 'montand)', 'does', 'not', 'believe', 'that', 'the', 'murder', 'was', 'planned', 'and', 'executed', 'by', 'the', 'single', 'man', 'daslow', '(=oswald)', 'and', 'though', 'all', 'other', 'officials', 'want', 'to', 'close', 'the', 'case', 'he', 'continuous', 'to', 'investigate', 'with', 'his', 'team.<br', '/><br', '/>the', 'screenplay', 'is', 'written', 'tight', 'and', 'fast', 'and', 'holds', 'the', 'tension', 'till', 'the', 'end.', 'just', 'the', 'part', 'dealing', 'with', 'the', 'milgram', 'experiment', 'about', 'authorities', 'is', '(though', 'not', 'uninteresting)', 'a', 'bit', 'out', 'of', 'place.', 'the', 'ending', 'sequence', '-', 'explaining', 'who', 'icarus', 'really', 'is', '-', 'partly', 'shot', 'in', 'slow', 'motion', 'and', 'intensified', 'by', 'a', 'morricone', 'soundtrack', 'is', 'the', 'most', 'powerful', 'sequence', 'i', 'have', 'ever', 'seen', 'in', 'a', 'movie.'], 'label': 'pos'}\n"
     ]
    }
   ],
   "source": [
    "print('train.fields', train.fields)\n",
    "print('len(train)', len(train))\n",
    "print('vars(train[0])', vars(train[0]))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "TEXT.build_vocab(train, vectors=GloVe(name='6B', dim=300),max_size=10000,min_freq=10)\n",
    "LABEL.build_vocab(train,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'neg': 12500, 'pos': 12500})"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LABEL.vocab.freqs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train.fields {'text': <torchtext.data.field.Field object at 0x7f6db748ef60>, 'label': <torchtext.data.field.Field object at 0x7f6db7c44630>}\n",
      "len(train) 25000\n",
      "vars(train[0]) {'text': [\"i'm\", 'not', 'tired', 'to', 'say', 'this', 'is', 'one', 'of', 'the', 'best', 'political', 'thrillers', 'ever', 'made.', 'the', 'story', 'takes', 'place', 'in', 'a', 'fictional', 'state,', 'but', 'obviously', 'it', 'deals', 'with', 'the', 'murder', 'of', 'kennedy.', 'a', 'truthful', 'and', 'honest', 'district', 'attorney', '(played', 'by', 'yves', 'montand)', 'does', 'not', 'believe', 'that', 'the', 'murder', 'was', 'planned', 'and', 'executed', 'by', 'the', 'single', 'man', 'daslow', '(=oswald)', 'and', 'though', 'all', 'other', 'officials', 'want', 'to', 'close', 'the', 'case', 'he', 'continuous', 'to', 'investigate', 'with', 'his', 'team.<br', '/><br', '/>the', 'screenplay', 'is', 'written', 'tight', 'and', 'fast', 'and', 'holds', 'the', 'tension', 'till', 'the', 'end.', 'just', 'the', 'part', 'dealing', 'with', 'the', 'milgram', 'experiment', 'about', 'authorities', 'is', '(though', 'not', 'uninteresting)', 'a', 'bit', 'out', 'of', 'place.', 'the', 'ending', 'sequence', '-', 'explaining', 'who', 'icarus', 'really', 'is', '-', 'partly', 'shot', 'in', 'slow', 'motion', 'and', 'intensified', 'by', 'a', 'morricone', 'soundtrack', 'is', 'the', 'most', 'powerful', 'sequence', 'i', 'have', 'ever', 'seen', 'in', 'a', 'movie.'], 'label': 'pos'}\n"
     ]
    }
   ],
   "source": [
    "print('train.fields', train.fields)\n",
    "print('len(train)', len(train))\n",
    "print('vars(train[0])', vars(train[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "d = vars(TEXT.vocab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['freqs', 'stoi', 'itos', 'vectors'])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 0.0000  0.0000  0.0000  ...   0.0000  0.0000  0.0000\n",
       " 0.0000  0.0000  0.0000  ...   0.0000  0.0000  0.0000\n",
       " 0.0466  0.2132 -0.0074  ...   0.0091 -0.2099  0.0539\n",
       "          ...             ⋱             ...          \n",
       " 0.0000  0.0000  0.0000  ...   0.0000  0.0000  0.0000\n",
       " 0.7724 -0.1800  0.2072  ...   0.6736  0.2263 -0.2919\n",
       " 0.0000  0.0000  0.0000  ...   0.0000  0.0000  0.0000\n",
       "[torch.FloatTensor of size 10002x300]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TEXT.vocab.vectors\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10002"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(TEXT.vocab.stoi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_iter, test_iter = data.BucketIterator.splits((train, test), batch_size=32, device=-1)\n",
    "\n",
    "train_iter.repeat = False\n",
    "test_iter.repeat = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class EmbNet(nn.Module):\n",
    "    def __init__(self,emb_size,hidden_size1,hidden_size2=400):\n",
    "        super().__init__()\n",
    "        self.embedding = nn.Embedding(emb_size,hidden_size1)\n",
    "        self.fc = nn.Linear(hidden_size2,3)\n",
    "        \n",
    "    def forward(self,x):\n",
    "        embeds = self.embedding(x).view(x.size(0),-1)\n",
    "        out = self.fc(embeds)\n",
    "        return F.log_softmax(out,dim=-1)\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model = EmbNet(len(TEXT.vocab.stoi),10)\n",
    "model = model.cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "optimizer = optim.Adam(model.parameters(),lr=0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_iter, test_iter = data.BucketIterator.splits((train, test), batch_size=32, device=-1,shuffle=True)\n",
    "train_iter.repeat = False\n",
    "test_iter.repeat = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fit(epoch,model,data_loader,phase='training',volatile=False):\n",
    "    if phase == 'training':\n",
    "        model.train()\n",
    "    if phase == 'validation':\n",
    "        model.eval()\n",
    "        volatile=True\n",
    "    running_loss = 0.0\n",
    "    running_correct = 0\n",
    "    for batch_idx , batch in enumerate(data_loader):\n",
    "        text , target = batch.text , batch.label\n",
    "        if is_cuda:\n",
    "            text,target = text.cuda(),target.cuda()\n",
    "        \n",
    "        if phase == 'training':\n",
    "            optimizer.zero_grad()\n",
    "        output = model(text)\n",
    "        loss = F.nll_loss(output,target)\n",
    "        \n",
    "        running_loss += F.nll_loss(output,target,size_average=False).data[0]\n",
    "        preds = output.data.max(dim=1,keepdim=True)[1]\n",
    "        running_correct += preds.eq(target.data.view_as(preds)).cpu().sum()\n",
    "        if phase == 'training':\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "    \n",
    "    loss = running_loss/len(data_loader.dataset)\n",
    "    accuracy = 100. * running_correct/len(data_loader.dataset)\n",
    "    \n",
    "    print(f'{phase} loss is {loss:{5}.{2}} and {phase} accuracy is {running_correct}/{len(data_loader.dataset)}{accuracy:{10}.{4}}')\n",
    "    return loss,accuracy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_losses , train_accuracy = [],[]\n",
    "val_losses , val_accuracy = [],[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/vishnu/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:9: DeprecationWarning: generator 'Iterator.__iter__' raised StopIteration\n",
      "  if __name__ == '__main__':\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "training loss is  0.74 and training accuracy is 12934/25000     51.74\n",
      "validation loss is   0.7 and validation accuracy is 13327/25000     53.31\n",
      "training loss is  0.69 and training accuracy is 14178/25000     56.71\n",
      "validation loss is  0.68 and validation accuracy is 14605/25000     58.42\n",
      "training loss is  0.64 and training accuracy is 15760/25000     63.04\n",
      "validation loss is  0.64 and validation accuracy is 15780/25000     63.12\n",
      "training loss is  0.59 and training accuracy is 16978/25000     67.91\n",
      "validation loss is  0.62 and validation accuracy is 16583/25000     66.33\n",
      "training loss is  0.55 and training accuracy is 17956/25000     71.82\n",
      "validation loss is   0.6 and validation accuracy is 17047/25000     68.19\n",
      "training loss is  0.51 and training accuracy is 18686/25000     74.74\n",
      "validation loss is  0.59 and validation accuracy is 17345/25000     69.38\n",
      "training loss is  0.48 and training accuracy is 19164/25000     76.66\n",
      "validation loss is  0.59 and validation accuracy is 17596/25000     70.38\n",
      "training loss is  0.45 and training accuracy is 19701/25000      78.8\n",
      "validation loss is  0.59 and validation accuracy is 17674/25000      70.7\n",
      "training loss is  0.43 and training accuracy is 20033/25000     80.13\n",
      "validation loss is  0.59 and validation accuracy is 17886/25000     71.54\n"
     ]
    }
   ],
   "source": [
    "train_losses , train_accuracy = [],[]\n",
    "val_losses , val_accuracy = [],[]\n",
    "\n",
    "for epoch in range(1,10):\n",
    "\n",
    "    epoch_loss, epoch_accuracy = fit(epoch,model,train_iter,phase='training')\n",
    "    val_epoch_loss , val_epoch_accuracy = fit(epoch,model,test_iter,phase='validation')\n",
    "    train_losses.append(epoch_loss)\n",
    "    train_accuracy.append(epoch_accuracy)\n",
    "    val_losses.append(val_epoch_loss)\n",
    "    val_accuracy.append(val_epoch_accuracy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using pretrained Glove word embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "TEXT = data.Field(lower=True, batch_first=True,fix_length=40,)\n",
    "LABEL = data.Field(sequential=False,)\n",
    "\n",
    "train, test = IMDB.splits(TEXT, LABEL)\n",
    "\n",
    "TEXT.build_vocab(train,test, vectors=GloVe(name='6B', dim=300),max_size=10000,min_freq=10)\n",
    "LABEL.build_vocab(train,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class EmbNet(nn.Module):\n",
    "    def __init__(self,emb_size,hidden_size1,hidden_size2=400):\n",
    "        super().__init__()\n",
    "        self.embedding = nn.Embedding(emb_size,hidden_size1)\n",
    "        self.fc1 = nn.Linear(hidden_size2,3)\n",
    "\n",
    "        \n",
    "    def forward(self,x):\n",
    "        embeds = self.embedding(x).view(x.size(0),-1)\n",
    "        out = self.fc1(embeds)\n",
    "        return F.log_softmax(out,dim=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model = EmbNet(len(TEXT.vocab.stoi),300,12000)\n",
    "model = model.cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.embedding.weight.data = TEXT.vocab.vectors.cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.embedding.weight.requires_grad = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#optimizer = optim.SGD(model.parameters(),lr=0.001)\n",
    "optimizer = optim.Adam([ param for param in model.parameters() if param.requires_grad == True],lr=0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_iter, test_iter = data.BucketIterator.splits((train, test), batch_size=64, device=-1,shuffle=True)\n",
    "train_iter.repeat = False\n",
    "test_iter.repeat = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fit(epoch,model,data_loader,phase='training',volatile=False):\n",
    "    if phase == 'training':\n",
    "        model.train()\n",
    "    if phase == 'validation':\n",
    "        model.eval()\n",
    "        volatile=True\n",
    "    running_loss = 0.0\n",
    "    running_correct = 0\n",
    "    for batch_idx , batch in enumerate(data_loader):\n",
    "        text , target = batch.text , batch.label\n",
    "        if is_cuda:\n",
    "            text,target = text.cuda(),target.cuda()\n",
    "        \n",
    "        if phase == 'training':\n",
    "            optimizer.zero_grad()\n",
    "        output = model(text)\n",
    "        loss = F.nll_loss(output,target)\n",
    "        \n",
    "        running_loss += F.nll_loss(output,target,size_average=False).data[0]\n",
    "        preds = output.data.max(dim=1,keepdim=True)[1]\n",
    "        running_correct += preds.eq(target.data.view_as(preds)).cpu().sum()\n",
    "        if phase == 'training':\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "    \n",
    "    loss = running_loss/len(data_loader.dataset)\n",
    "    accuracy = 100. * running_correct/len(data_loader.dataset)\n",
    "    \n",
    "    print(f'{phase} loss is {loss:{5}.{2}} and {phase} accuracy is {running_correct}/{len(data_loader.dataset)}{accuracy:{10}.{4}}')\n",
    "    return loss,accuracy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/vishnu/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:9: DeprecationWarning: generator 'Iterator.__iter__' raised StopIteration\n",
      "  if __name__ == '__main__':\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "training loss is  0.66 and training accuracy is 15727/25000     62.91\n",
      "validation loss is  0.64 and validation accuracy is 16489/25000     65.96\n",
      "training loss is  0.56 and training accuracy is 17809/25000     71.24\n",
      "validation loss is  0.66 and validation accuracy is 16496/25000     65.98\n",
      "training loss is  0.53 and training accuracy is 18366/25000     73.46\n",
      "validation loss is  0.67 and validation accuracy is 16524/25000      66.1\n",
      "training loss is   0.5 and training accuracy is 18825/25000      75.3\n",
      "validation loss is   0.7 and validation accuracy is 16416/25000     65.66\n",
      "training loss is  0.49 and training accuracy is 18990/25000     75.96\n",
      "validation loss is  0.72 and validation accuracy is 16368/25000     65.47\n",
      "training loss is  0.48 and training accuracy is 19181/25000     76.72\n",
      "validation loss is  0.73 and validation accuracy is 16354/25000     65.42\n",
      "training loss is  0.47 and training accuracy is 19406/25000     77.62\n",
      "validation loss is  0.75 and validation accuracy is 16231/25000     64.92\n",
      "training loss is  0.46 and training accuracy is 19646/25000     78.58\n",
      "validation loss is  0.77 and validation accuracy is 16188/25000     64.75\n",
      "training loss is  0.45 and training accuracy is 19641/25000     78.56\n",
      "validation loss is  0.81 and validation accuracy is 16102/25000     64.41\n"
     ]
    }
   ],
   "source": [
    "for epoch in range(1,10):\n",
    "\n",
    "    epoch_loss, epoch_accuracy = fit(epoch,model,train_iter,phase='training')\n",
    "    val_epoch_loss , val_epoch_accuracy = fit(epoch,model,test_iter,phase='validation')\n",
    "    train_losses.append(epoch_loss)\n",
    "    train_accuracy.append(epoch_accuracy)\n",
    "    val_losses.append(val_epoch_loss)\n",
    "    val_accuracy.append(val_epoch_accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/vishnu/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:9: DeprecationWarning: generator 'Iterator.__iter__' raised StopIteration\n",
      "  if __name__ == '__main__':\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "training loss is  0.45 and training accuracy is 19673/25000     78.69\n",
      "validation loss is   0.8 and validation accuracy is 16174/25000      64.7\n",
      "training loss is  0.45 and training accuracy is 19747/25000     78.99\n",
      "validation loss is  0.82 and validation accuracy is 16182/25000     64.73\n",
      "training loss is  0.44 and training accuracy is 19899/25000      79.6\n",
      "validation loss is  0.83 and validation accuracy is 16145/25000     64.58\n",
      "training loss is  0.43 and training accuracy is 20005/25000     80.02\n",
      "validation loss is  0.85 and validation accuracy is 16031/25000     64.12\n",
      "training loss is  0.42 and training accuracy is 20079/25000     80.32\n",
      "validation loss is  0.86 and validation accuracy is 16071/25000     64.28\n",
      "training loss is  0.42 and training accuracy is 20103/25000     80.41\n",
      "validation loss is  0.87 and validation accuracy is 16063/25000     64.25\n",
      "training loss is  0.42 and training accuracy is 20107/25000     80.43\n",
      "validation loss is  0.89 and validation accuracy is 16067/25000     64.27\n",
      "training loss is  0.41 and training accuracy is 20246/25000     80.98\n",
      "validation loss is  0.93 and validation accuracy is 15827/25000     63.31\n",
      "training loss is  0.42 and training accuracy is 20180/25000     80.72\n",
      "validation loss is  0.95 and validation accuracy is 15868/25000     63.47\n"
     ]
    }
   ],
   "source": [
    "for epoch in range(1,10):\n",
    "\n",
    "    epoch_loss, epoch_accuracy = fit(epoch,model,train_iter,phase='training')\n",
    "    val_epoch_loss , val_epoch_accuracy = fit(epoch,model,test_iter,phase='validation')\n",
    "    train_losses.append(epoch_loss)\n",
    "    train_accuracy.append(epoch_accuracy)\n",
    "    val_losses.append(val_epoch_loss)\n",
    "    val_accuracy.append(val_epoch_accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
